package com.syezon.wifikey.pwd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.pwd.info.CommonPwdInfo;
import com.syezon.wifikey.utils.ClipboardUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/9/6.
 */

public class CommonPwdAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<CommonPwdInfo> mData = new ArrayList<>();

    public CommonPwdAdapter(Context context) {
        this.mContext = context;
        setData(getData());
    }

    public void setData(List<CommonPwdInfo> data) {
        if (data != null && data.size() > 0) {
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    private List<CommonPwdInfo> getData() {
        List<CommonPwdInfo> data = new ArrayList<>();
        List<String> pwdList = new ArrayList<>();
        pwdList.add("12345678");
        pwdList.add("123456789");
        pwdList.add("88888888");
        pwdList.add("1234567890");
        pwdList.add("87654321");
        pwdList.add("11111111");
        pwdList.add("00000000");
        pwdList.add("987654321");
        pwdList.add("66668888");
        pwdList.add("11223344");
        pwdList.add("0123456789");
        pwdList.add("147258369");
        pwdList.add("66666666");
        pwdList.add("123123123");
        pwdList.add("8888888888");
        pwdList.add("12341324");
        pwdList.add("12344321");
        pwdList.add("88889999");
        pwdList.add("11112222");
        pwdList.add("a1b2c3d4");
        pwdList.add("22222222");
        pwdList.add("99999999");
        pwdList.add("999999999");
        pwdList.add("11111111111");
        pwdList.add("12345679");
        pwdList.add("0987654321");
        pwdList.add("77777777");
        pwdList.add("23456789");
        pwdList.add("1122334455");
        pwdList.add("9876543210");
        pwdList.add("55555555");
        pwdList.add("qwertyuiop");
        pwdList.add("8888888888");
        pwdList.add("111111111");
        pwdList.add("12345687");
        pwdList.add("012345678");
        pwdList.add("abcd1234");
        pwdList.add("01234567");
        pwdList.add("a123456789");
        pwdList.add("0000000000");
        for (int i = 0; i < pwdList.size(); i++) {
            CommonPwdInfo commonPwdInfo = new CommonPwdInfo();
            String pwd = pwdList.get(i);
            commonPwdInfo.setPwd(pwd);
            commonPwdInfo.setRank((i + 1));
            commonPwdInfo.setType(CommonPwdInfo.TYPE_PWD);
            data.add(commonPwdInfo);
        }
        CommonPwdInfo commonPwdInfo = new CommonPwdInfo();
        commonPwdInfo.setType(CommonPwdInfo.TYPE_TIP);
        data.add(0, commonPwdInfo);
        return data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == CommonPwdInfo.TYPE_PWD) {
            return new CommonPwdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_common_pwd, parent, false));
        }
        if (viewType == CommonPwdInfo.TYPE_TIP) {
            return new TipViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_common_pwd_tip, parent, false));
        }
        return new CommonPwdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_common_pwd, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final CommonPwdInfo commonPwdInfo = mData.get(position);
        if (commonPwdInfo.getType() == CommonPwdInfo.TYPE_PWD) {
            CommonPwdViewHolder viewHolder = (CommonPwdViewHolder) holder;
            viewHolder.tvRank.setText("TOP" + commonPwdInfo.getRank());
            viewHolder.tvPwd.setText(commonPwdInfo.getPwd());
            viewHolder.tvCopy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClipboardUtils.copyText(commonPwdInfo.getPwd());
                    ToastUtils.showShort("复制成功！");
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        CommonPwdInfo commonPwdInfo = mData.get(position);
        return commonPwdInfo.getType();
    }

    public static class TipViewHolder extends RecyclerView.ViewHolder {

        public TipViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class CommonPwdViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_rank)
        TextView tvRank;
        @BindView(R.id.tv_pwd)
        TextView tvPwd;
        @BindView(R.id.tv_copy)
        TextView tvCopy;

        public CommonPwdViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
